<?php

//进程事件

//创建进程池
$workers = []; //进程池
$worker_num = 3; //进程池数量

for ($i = 0; $i < $worker_num; $i++) {
    $process = new swoole_process('doProcess');  //创建单独的进程
    $pid = $process->start();  //启动进程，并返回进程ID
    $workers[$pid] = $process; //将进程ID和进程对象存入数组
}

//创建进程执行函数
function doProcess(swoole_process $process) {
    $process->write("PID: $process->pid ");  //向进程写入数据
    echo "写入信息: $process->pid $process->callback \n";
    //$process->exit();
    // $process->close(); //关闭标准输入输出流
    // $process->free(); //释放资源
    // $process->detach(); //脱离父进程
    // $process->wait(); //等待子进程退出
    // $process->kill(); //杀掉子进程
    // $process->signal(); //向子进程发送信号
    // $process->alarm(); //设置定时器
    // $process->exit(); //退出子进程
    // $process->exec(); //执行外部程序
    // $process->setAffinity(); //设置CPU亲和性
    // $process->getProcess(); //获取进程对象
    // $process->getProcessId(); //获取进程ID
    // $process->getProcessName(); //获取进程名称
}

// 添加进程事件 向每一个子进程添加需要执行的动作
foreach ($workers as $process) {
    // swoole_event::add($process->pipe, function ($pipe) use ($process) {
    //     echo "收到信息: $process->id \n";
    // });
    swoole_event_add($process->pipe, function ($pipe) use ($process) {
        //echo "收到信息: $process->pid \n";
        $data = $process->read();
        echo "接收到: $data \n";
    });
}